package net.pantolomin.nayo.api;

import java.io.IOException;
import java.net.InetAddress;

/**
 * A listener for events
 */
public interface ConnectionEventsListener {
	/**
	 * Indicates that a new connection has been established. This method is called only by the "selection thread".
	 * 
	 * @param connection
	 *          the connection
	 */
	void newConnection(Connection connection);

	/**
	 * Indicates that the "bind" was successful. This method is called only by the "selection thread".
	 * 
	 * @param hostAddress
	 *          the local host's address
	 * @param port
	 *          the local port to bind on
	 */
	void bindDone(InetAddress hostAddress, int port);

	/**
	 * Indicates that the "bind" failed. This method is called only by the "selection thread".
	 * 
	 * @param hostAddress
	 *          the local host's address
	 * @param port
	 *          the local port to bind on
	 * @param cause
	 *          the cause
	 */
	void bindFailed(InetAddress hostAddress, int port, String cause);

	/**
	 * Indicates that a connection attempt timed-out. This method is called only by the "selection thread".
	 * 
	 * @param hostAddress
	 *          the remote host's address
	 * @param port
	 *          the remote port to connect to
	 */
	void connectionTimeout(InetAddress hostAddress, int port);

	/**
	 * Indicates that a connection failed. This method is called only by the "selection thread".
	 * 
	 * @param hostAddress
	 *          the remote host's address
	 * @param port
	 *          the remote port to connect to
	 * @param cause
	 *          the cause
	 */
	void connectionFailed(InetAddress hostAddress, int port, String cause);

	/**
	 * Indicates that a connection was closed. This method is called only by the "selection thread".
	 * 
	 * @param connection
	 *          the connection
	 */
	void connectionClosed(Connection connection);

	/**
	 * Indicates that a connection was closed by the remote host.
	 * 
	 * @param connection
	 *          the connection
	 * @param cleanly
	 *          indicates if the connection was closed cleanly
	 */
	void closedByRemote(Connection connection, boolean cleanly);

	/**
	 * Notifies that some data was read. This method is called only once at a time for a particular connection.
	 * 
	 * @param connection
	 *          the connection
	 * @param data
	 *          the read data
	 */
	void dataRead(Connection connection, byte[] data);

	/**
	 * Indicates that writing some data failed. This method is called only once at a time for a particular connection.
	 * 
	 * @param connection
	 *          the connection
	 * @param exception
	 *          the problem that occurred
	 */
	void writeFailed(Connection connection, IOException exception);

	/**
	 * Indicates that the buffer for outgoing data is full. The connection may be too slow for the amount of data we are sending. This method is called only once
	 * at a time for a particular connection.
	 * 
	 * @param connection
	 *          the connection
	 */
	void writeBufferFull(Connection connection);

	/**
	 * Indicates that the buffer for outgoing data is available again. The connection is now able to flush the data waiting to be written. This method is called
	 * only once at a time for a particular connection.
	 * 
	 * @param connection
	 *          the connection
	 */
	void writeBufferFreed(Connection connection);
}